<template>
	<view>
		<!-- #ifdef MP || APP-PLUS -->
		<NavBar titleText="抽奖活动" :iconColor="iconColor" :textColor="iconColor" :isScrolling="isScrolling" showBack></NavBar>
		<!-- #endif -->
		<view class="page-wrapper" v-if="lotteryShow && loading">
			<template1 v-if="lotteryType==1" :type="type" :image="image" :isAll="is_all_record" :isPersonal="is_personal_record" :isContent="is_content" :prizeList="prize" :userList="userList.data"
				:myList="myList.data" :htmlData="htmlData.data" :lotteryNum="lottery_num" @getWiningIndex="getWiningIndex" @luck_draw_finish="luck_draw_finish" @H5Share="H5Share">
			</template1>
			<template2 v-else-if="lotteryType==2" :type="type" :image="image" :isAll="is_all_record" :isPersonal="is_personal_record" :isContent="is_content" :prizeList="prize"
				:userList="userList.data" :myList="myList.data" :htmlData="htmlData.data" :lotteryNum="lottery_num" @getWiningIndex="getWiningIndex" @luck_draw_finish="luck_draw_finish"
				@H5Share="H5Share">
			</template2>
			<view v-if="rulesShow || aleartStatus || addressModel" class="mask" @touchmove.stop.prevent="moveHandle"></view>
		</view>
		<view class="p-20" v-else-if="!lotteryShow && loading">
			<emptyPage title="商家暂未上架活动哦～" src="/statics/images/noActivity.gif"></emptyPage>
		</view>
		<!-- 抽奖结果弹窗 -->
		<lotteryAleart :aleartStatus="aleartStatus" @close="closeLottery" :alData="alData" :aleartType="aleartType">
		</lotteryAleart>
		<!-- #ifdef H5 -->
		<view class="share-box" v-if="H5ShareBox">
			<image :src="imgHost + '/statics/images/share-info.png'" @click="H5ShareBox = false"></image>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		getLotteryData,
		startLottery,
		receiveLottery
	} from '@/api/lottery.js'
	import {
		getUserInfo
	} from '@/api/user.js';
	import colors from '@/mixins/color.js'
	import zbCode from '@/components/zb-code/zb-code.vue'
	import gridsLottery from '../../components/lottery/index.vue'
	import showBox from '../components/showbox.vue'
	import noticeBar from '../components/noticeBar.vue'
	import lotteryAleart from '../../components/lotteryAleart/index.vue'
	import home from '@/components/home';
	import template1 from '../template/template1.vue';
	import template2 from '../template/template2.vue';
	import emptyPage from '@/components/emptyPage.vue';
	// #ifdef MP || APP-PLUS
	import NavBar from '@/components/NavBar.vue';
	// #endif
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		postCartAdd
	} from '@/api/store.js';
	import {
		mapGetters
	} from "vuex";
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	const app = getApp();
	export default {
		components: {
			template1,
			template2,
			gridsLottery,
			showBox,
			noticeBar,
			lotteryAleart,
			zbCode,
			emptyPage,
			home,
			// #ifdef MP || APP-PLUS
			NavBar,
			// #endif
		},
		mixins: [colors],
		data() {
			return {
				lotteryShow: true,
				loading: false,
				H5ShareBox: false,
				// #ifdef H5
				isWeixin: this.$wechat.isWeixin(),
				// #endif
				addressModel: false,
				lottery_num: 0,
				aleartType: 0,
				aleartStatus: false,
				lottery_draw_param: {
					startIndex: 3, //开始抽奖位置，从0开始
					totalCount: 3, //一共要转的圈数
					winingIndex: 1, //中奖的位置，从0开始
					speed: 100 //抽奖动画的速度 [数字越大越慢,默认100]
				},
				userList: {
					type: 'user',
					data: []
				},
				myList: {
					type: 'me',
					data: []
				},
				htmlData: {
					type: 'html',
					data: ''
				},
				prizeData: {
					type: 'prize',
					data: []
				},
				prize: [],
				factor_num: 0,
				id: 0,
				alData: {},
				type: '',
				followCode: false,
				//二维码参数
				codeShow: false,
				cid: '1',
				ifShow: true,
				val: "", // 要生成的二维码值
				lv: 3, // 二维码容错级别 ， 一般不用设置，默认就行
				onval: true, // val值变化时自动重新生成二维码
				loadMake: true, // 组件加载完成后自动生成二维码
				src: '', // 二维码生成后的图片地址或base64
				codeSrc: "",
				image: "", //上部背景图
				is_content: 0,
				is_all_record: 0,
				is_personal_record: 0,
				factor: 0,
				imgHost: HTTP_REQUEST_URL,
				isShowAuth: false,
				rulesShow: false,
				isRotating: false,
				lotteryType: 0,
				iconColor: '#FFFFFF',
				isScrolling: false,
			}
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						//#ifndef MP
						this.getLotteryData(this.type)
						//#endif
					}
				},
				deep: true
			}
		},
		onLoad(option) {
			this.type = option.type;
			if (this.isLogin) {
				this.getLotteryData(this.type)
			}
		},
		onShow() {
			uni.removeStorageSync('form_type_cart');
			if (!this.isLogin) {
				toLogin()
			}
		},
		onPageScroll(e) {
			// #ifdef MP
			if (e.scrollTop > 50) {
				this.iconColor = '#333333';
				this.isScrolling = true;
			} else {
				this.iconColor = '#FFFFFF';
				this.isScrolling = false;
			}
			// #endif
		},
		methods: {
			H5Share() {
				this.H5ShareBox = true;
			},
			onLoadFun() {
				this.getLotteryData(this.type)
				this.isShowAuth = false
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			//#ifdef H5
			ShareInfo(data) {
				let href = location.href;
				if (this.$wechat.isWeixin()) {
					getUserInfo().then(res => {
						href = href.indexOf('?') === -1 ? href + '?spid=' + res.data.uid : href + '&spid=' +
							res.data.uid;
						let configAppMessage = {
							desc: data.name,
							title: data.name,
							link: href,
							imgUrl: data.image
						};
						this.$wechat
							.wechatEvevt(['updateAppMessageShareData', 'updateTimelineShareData',
								'onMenuShareAppMessage',
								'onMenuShareTimeline'
							], configAppMessage)
							.then(res => {})
							.catch(err => {});
					});
				}
			},
			//#endif
			getLotteryData(type) {
				uni.showLoading({
					title: '获取抽奖信息'
				});
				getLotteryData(type).then(res => {
					this.loading = true
					this.factor_num = res.data.lottery.factor_num
					this.id = res.data.lottery.id
					this.image = res.data.lottery.image
					this.prize = res.data.lottery.prize
					this.lottery_num = res.data.lottery_num
					this.htmlData.data = res.data.lottery.content
					this.is_content = res.data.lottery.is_content
					this.is_personal_record = res.data.lottery.is_personal_record
					this.is_all_record = res.data.lottery.is_all_record
					this.factor = res.data.lottery.factor
					this.userList.data = res.data.all_record
					this.myList.data = res.data.user_record
					this.lotteryType = res.data.lottery.type
					if (this.lotteryType == 1) {
						this.prize.push({})
					}
					// #ifdef H5
					if (this.isLogin) {
						this.ShareInfo(res.data.lottery);
					}
					// #endif
					this.prizeData.data = res.data.lottery.prize;
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
					this.lotteryShow = false
					this.loading = true
					this.$util.Tips({
						title: err
					});
				})
			},
			closeLottery(status) {
				this.aleartStatus = false
				this.getLotteryData(this.type)
				if (this.alData.type === 6) {
					postCartAdd({
						cartNum: 1,
						new: 1,
						is_new: 1,
						productId: this.alData.product_id,
						uniqueId: this.alData.unique,
						luckRecordId: this.alData.lottery_record_id,
					}).then(res => {
						uni.navigateTo({
							url: `/pages/goods/order_confirm/index?new=1&luckRecordId=${this.alData.lottery_record_id}&cartId=${res.data.cartId}`
						});
					}).catch(err => {
						this.$util.Tips({
							title: `${err},请联系客服`
						});
					});;
				}
			},
			getAddress(data) {
				let addData = data
				addData.id = this.alData.lottery_record_id
				addData.address = data.address.province + data.address.city + data.address.district + data.detail
				receiveLottery(addData).then(res => {
					this.$util.Tips({
						title: '领取成功'
					});
					this.addressModel = false
				}).catch(err => {
					this.$util.Tips({
						title: err
					});
				})
			},
			getWiningIndex(callback) {
				this.aleartType = 0
				let that = this
				startLottery({
					id: this.id,
					type: this.type
				}).then(res => {
					if (res.data.code === 'subscribe') {
						that.$set(that, 'followCode', true);
						this.codeSrc = res.data.url
						return;
					}
					this.prize.forEach((item, index) => {
						if (res.data.id === item.id) {
							this.alData = res.data
							this.lottery_draw_param.winingIndex = index;
							callback(this.lottery_draw_param);
						}
					})
				}).catch(err => {
					this.$util.Tips({
						title: err
					});
				})
				// //props修改在小程序和APP端不成功，所以在这里使用回调函数传参，
			},
			// 抽奖完成
			luck_draw_finish(param) {
				this.aleartType = 2
				this.aleartStatus = true
				this.isRotating = false
				// console.log(`抽到第${param+1}个方格的奖品`)
			},
			qrR(res) {
				this.codeSrc = res
			},
			moveHandle() {},
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .lucky-wheel .marquee-item {
		text-align: center;
		font-size: 20rpx !important;
		color: #E93323 !important;
		letter-spacing: 2px;

		.name {
			color: #E93323 !important;
		}
	}

	/deep/ .lottery .marquee-item {
		letter-spacing: 2px;
	}

	.headerBg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.page-wrapper {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	/deep/ .safe-area-inset-bottom {
		height: 0;
		height: constant(safe-area-inset-bottom);
		height: env(safe-area-inset-bottom);
	}

	.lottery {
		background-color: #E74435;
		min-height: 100vh;
		padding: 0 0 20rpx 0;

		.mask {
			z-index: 1000;
		}

		.rules-popup {
			position: fixed;
			top: 50%;
			left: 50%;
			z-index: 1001;
			display: none;
			width: 620rpx;
			padding: 94rpx 0 0;
			border: 10rpx solid #FED59C;
			border-radius: 48rpx;
			background-color: #FFFFFF;
			transform: translate(-50%, -50%);

			&.active {
				display: block;
			}

			.popup-top {
				position: absolute;
				top: -24rpx;
				left: 50%;
				width: 396rpx;
				height: 87rpx;
				padding-top: 14rpx;
				background-image: url();
				background-repeat: no-repeat;
				background-size: 100% 100%;
				transform: translateX(-50%);
				text-align: center;

				.title {
					display: inline-block;
					padding: 0 48rpx;
					background-image: url(),
						url();
					background-position: left center, right center;
					background-repeat: no-repeat;
					background-size: 37rpx 13rpx;
					font-weight: 500;
					font-size: 36rpx;
					line-height: 50rpx;
					color: #E93323;
				}
			}

			.popup-center {
				height: 572rpx;
				padding: 0 38rpx;
			}

			.popup-bottom {
				padding: 24rpx 40rpx;

				.button {
					height: 80rpx;
					border-radius: 40rpx;
					background-color: #E93323;
					font-weight: 500;
					font-size: 28rpx;
					color: #FFFFFF;
				}
			}
		}

		.rules-button {
			position: absolute;
			top: 198rpx;
			right: 0;
			width: 52rpx;
			padding: 10rpx 15rpx;
			border-radius: 16rpx 0 0 16rpx;
			background: linear-gradient(90deg, #FF8D8D 0%, #FF3F3F 100%);
			font-weight: 500;
			font-size: 22rpx;
			line-height: 30rpx;
			color: #FFFFFF;
		}

		.lottery-header {
			width: 100%;
			height: 580rpx;
			margin: 0;
		}

		.grids {
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			position: relative;
			padding: 448rpx 85rpx 0;
			background-image: url();
			background-repeat: no-repeat;
			background-size: 100%;


			.grids-bag {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
			}

			.grids-box {
				width: 540rpx;
				height: 540rpx;
				// z-index: 10000;
				margin-top: 59rpx;
			}

			.grids-top {
				display: flex;

				image {
					width: 40rpx;
					height: 40rpx;
				}

				.grids-title {
					display: flex;
					justify-content: center;
					width: 100%;
					font-size: 20px;
					color: #fff;
					z-index: 999;
					padding: 0 14rpx;

					.grids-frequency {
						color: #FFD68E;
					}
				}
			}

			.winning-tips-list {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 50%;
				font-size: 20rpx;
				line-height: 62rpx;
				height: 62rpx;
				font-weight: 400;
				color: #FFF8F8;
				// margin: 20rpx 0;
				z-index: 999;
				// background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.3) 51%, rgba(255, 255, 255, 0) 100%);

				.iconfont {
					font-size: 20rpx;
					margin-right: 10rpx;
				}
			}
		}

		.invite-people {
			display: flex;
			justify-content: center;

			.invite {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 558rpx;
				height: 76rpx;
				font-size: 32rpx;
				font-weight: 600;
				color: #E74435;
				background: #FFD68E;
				box-shadow: 0px 6px 0px 0px rgba(185, 16, 0, 0.3);
				border-radius: 38px;
				margin: 76rpx;
			}
		}
	}

	.mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.8);
		z-index: 9;
	}

	.share-box {
		z-index: 1300;
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.followCode {
		.pictrue {
			width: 500rpx;
			height: 530rpx;
			border-radius: 12px;
			left: 50%;
			top: 50%;
			margin-left: -250rpx;
			margin-top: -360rpx;
			position: fixed;
			z-index: 10000;

			.code-bg {
				display: flex;
				justify-content: center;
				width: 100%;
				height: 100%;
				background-image: url('~@/static/images/code-bg.png');
				background-size: 100% 100%;
			}

			.imgs {
				width: 310rpx;
				height: 310rpx;
				margin-top: 92rpx;
			}
		}

		.mask {
			z-index: 9999;
		}
	}

	[v-cloak] {
		display: none;
	}

	.draw-button {
		width: 470rpx;
		height: 141rpx;
		margin: 88rpx 0 52rpx;
		background-image: url();
		background-size: 100% 100%;
	}

	.show-box-wrapper {
		padding: 20rpx;
		background-color: #EC4545;
	}

	.show-box-list {
		padding: 30rpx 20rpx;
		border-radius: 24rpx;
		background: #E23B1F;
		box-shadow: inset 0rpx 1rpx 3rpx 0rpx rgba(0, 0, 0, 0.5);
	}
</style>